﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using ScriptCoreLib;
using ScriptCoreLib.ActionScript;
using ScriptCoreLib.ActionScript.Extensions;
using ScriptCoreLib.ActionScript.flash.display;
using ScriptCoreLib.ActionScript.flash.text;
//using ScriptCoreLib.ActionScript.MochiLibrary;
using ScriptCoreLib.Shared;
using ScriptCoreLib.Shared.Avalon.Extensions;

namespace AvalonTycoonMansion.ActionScript
{
	using TargetCanvas = global::AvalonTycoonMansion.Shared.OrcasAvalonApplicationCanvas;
	using AvalonTycoonMansion.Shared.Promotion;
    //using ScriptCoreLib.ActionScript.MochiLibrary.Bot;


	/// <summary>
	/// Default flash player entrypoint class. See 'tools/build.bat' for adding more entrypoints.
	/// </summary
	[Script]

	[ScriptApplicationEntryPoint(
		Width = TargetCanvas.DefaultWidth, 
		Height = TargetCanvas.DefaultHeight,
		Background = true,
		BackgroundColor = 0,
		AlignToCenter = true,

		// we are not opting in for automatic resource embedding... yet
		// now we are:)
		WithResources = true
	)]

	[SWF(
		width = TargetCanvas.DefaultWidth, 
		height = TargetCanvas.DefaultHeight, 
		frameRate = 160, 
		backgroundColor = 0
	)]

    //[Frame(typeof(MochiPreloader))]

	[GoogleGadget(
	   author_email = Info.EMail,
	   author_link = Info.Blog,
	   author = Info.Author,
	   category = Info.Category1,
	   category2 = Info.Category2,
	   screenshot = Info.ScreenshotURL,
	   thumbnail = Info.ScreenshotSmallURL,
	   description = Info.Description,
	   width = TargetCanvas.DefaultWidth,
	   height = TargetCanvas.DefaultHeight,
	   title = Info.Title,

	   // Note that all domain names that receive less than 25 hits per 90 days will be removed from your account.
	   title_url = Info.Web,
	   src = Info.Source
	)]

	public class OrcasAvalonApplicationFlash : Sprite
	{
		public OrcasAvalonApplicationFlash()
		{
			this.InvokeWhenStageIsReady(
				delegate
				{
                    //MochiBot.track(this, Info.MochiBotKey);
				}
			);

			var c = new TargetCanvas();

            c.ToFullscreen =
                delegate
                {
                    this.stage.SetFullscreen(true);
                };

			c.SetHighscore =
				score =>
				{
                    //new MochiScoresOptions
                    //{
                    //    boardID = Info.MochiScoresKey,
                    //    score = score
                    //}.showLeaderboard();
				};

			// spawn the wpf control
            c.AttachToContainer(this);

            this.InvokeWhenStageIsReady(
                delegate
                {
                    this.stage.resize +=
                        e =>
                        {

                            c.SizeTo(this.stage.stageWidth, this.stage.stageHeight);
                        };
                }
            );

		}

		static OrcasAvalonApplicationFlash()
		{
			
			// add resources to be found by ImageSource
			//KnownEmbeddedAssets.RegisterTo(
			//    KnownEmbeddedResources.Default.Handlers
			//);

		}
	}

//    [Script]
//    public class MochiPreloader : MochiAdPreloader
//    {
//        [TypeOfByNameOverride]
//        public override Type GetTargetType()
//        {
//            return typeof(OrcasAvalonApplicationFlash);
//        }


//        public override bool IsBackgroundVisible()
//        {
//            return false;
//        }

//        public override bool AdsEnabled()
//        {
//#if PUBLISH
//            return true;
//#else
//            return false;
//#endif
//        }

//        [Embed(source = "/assets/AvalonTycoonMansion/preview.jpg", mimeType = "image/jpeg")]
//        internal static Class CustomBackground;

//        public MochiPreloader()
//            : base(AvalonTycoonMansion.Shared.Promotion.Info.MochiAdsKey)
//        {
//        }

//        public override void InitializeBackground()
//        {
//            CustomBackground.ToBitmapAsset().AttachTo(this);
//        }
//    }

	[Script]
	public class KnownEmbeddedAssets
	{
		[EmbedByFileName]
		public static Class ByFileName(string e)
		{
			throw new NotImplementedException();
		}

		public static void RegisterTo(List<Converter<string, Class>> Handlers)
		{
			// assets from current assembly
			Handlers.Add(e => ByFileName(e));

			//// assets from referenced assemblies
			global::ScriptCoreLib.ActionScript.Avalon.Carousel.KnownEmbeddedAssets.RegisterTo(Handlers);
			//Handlers.Add(e => global::ScriptCoreLib.ActionScript.Avalon.Cursors.EmbeddedAssets.Default[e]);
			//Handlers.Add(e => global::ScriptCoreLib.ActionScript.Avalon.TiledImageButton.Assets.Default[e]);

		}
	}
}